package com.crm.miaohe.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.http.HttpHeaders;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@EnableWebMvc
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Value("${spring.messages.basename}")
    private String[] basename;

    @Value("${spring.messages.encoding}")
    private String encoding;

    @Override
    protected Validator getValidator() {
        return localValidator();
    }

    @Bean
    public LocalValidatorFactoryBean localValidator() {
        LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
        validator.setValidationMessageSource(this.messageSource());
        return validator;
    }

//    @Bean
//    public Jackson2ObjectMapperBuilderCustomizer objectMapper() {
//        return builder -> {
//            builder.indentOutput(true);
//            builder.featuresToEnable(MapperFeature.USE);
//            builder.propertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);
//            builder.visibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
//            builder.visibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
//        };
//    }

    /**
     * @return {@link MessageSource}
     */
    private MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource =
                new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames(this.basename);
        messageSource.setDefaultEncoding(this.encoding);
        return messageSource;
    }

    @Bean
    public CorsFilter corsFilter(){
        //1.添加Cors配置信息

        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOriginPattern("*");
        config.addAllowedOrigin("http://192.168.10.10:8081");

        //设置是否发送cookie信息
        config.setAllowCredentials(true);

        //设置允许的header
        config.addAllowedHeader("*");
        config.addExposedHeader("*");

        //设置自定义的响应头
        config.addExposedHeader(HttpHeaders.AUTHORIZATION);

        //设置允许的header
        config.addAllowedMethod("*");

        //2. 为url添加映射路径
        UrlBasedCorsConfigurationSource corsSource = new UrlBasedCorsConfigurationSource();
        corsSource.registerCorsConfiguration("/**",config);

        //3.返回重新定义好的corsSource
        return new CorsFilter(corsSource);
    }


}
